package com.maserb.monitor.dataCenter.communication;


import java.util.Arrays;

public class ByteUtils {
    /**
     * 转换short为byte
     *
     * @param b
     * @param s 需要转换的short
     */
    public static void putShort(byte b[], short s, Box<Integer> index) {
        b[index.value + 1] = (byte) (s >> 8);
        b[index.value + 0] = (byte) (s >> 0);
        index.value += 8;
    }

    /**
     * 通过byte数组取到short
     *
     * @param b
     * @return
     */
    public static short getShort(byte[] b, Box<Integer> index) {
        short result = (short) (((b[index.value + 1] << 8) | b[index.value + 0] & 0xff));
        index.value += 2;
        return result;
    }

    /**
     * 转换int为byte数组
     *
     * @param bb
     * @param x
     */
    public static void putInt(byte[] bb, int x, Box<Integer> index) {
        bb[index.value + 3] = (byte) (x >> 24);
        bb[index.value + 2] = (byte) (x >> 16);
        bb[index.value + 1] = (byte) (x >> 8);
        bb[index.value + 0] = (byte) (x >> 0);
        index.value += 8;
    }

    /**
     * 通过byte数组取到int
     *
     * @param bb
     * @return
     */
    public static int getInt(byte[] bb, Box<Integer> index) {
        int result = (int) ((((bb[index.value + 3] & 0xff) << 24)
                | ((bb[index.value + 2] & 0xff) << 16)
                | ((bb[index.value + 1] & 0xff) << 8) | ((bb[index.value + 0] & 0xff) << 0)));
        index.value += 4;
        return result;
    }

    /**
     * 转换long型为byte数组
     *
     * @param bb
     * @param x
     */
    public static void putLong(byte[] bb, long x, Box<Integer> index) {
        bb[index.value + 7] = (byte) (x >> 56);
        bb[index.value + 6] = (byte) (x >> 48);
        bb[index.value + 5] = (byte) (x >> 40);
        bb[index.value + 4] = (byte) (x >> 32);
        bb[index.value + 3] = (byte) (x >> 24);
        bb[index.value + 2] = (byte) (x >> 16);
        bb[index.value + 1] = (byte) (x >> 8);
        bb[index.value + 0] = (byte) (x >> 0);
        index.value += 8;
    }

    /**
     * 通过byte数组取到long
     *
     * @param bb
     * @return
     */
    public static long getLong(byte[] bb, Box<Integer> index) {
        long result = ((((long) bb[index.value + 7] & 0xff) << 56)
                | (((long) bb[index.value + 6] & 0xff) << 48)
                | (((long) bb[index.value + 5] & 0xff) << 40)
                | (((long) bb[index.value + 4] & 0xff) << 32)
                | (((long) bb[index.value + 3] & 0xff) << 24)
                | (((long) bb[index.value + 2] & 0xff) << 16)
                | (((long) bb[index.value + 1] & 0xff) << 8) | (((long) bb[index.value + 0] & 0xff) << 0));

        index.value += 8;
        return result;
    }

    /**
     * 字符到字节转换
     *
     * @param ch
     * @return
     */
    public static void putChar(byte[] bb, char ch, Box<Integer> index) {
        int temp = (int) ch;
        // byte[] b = new byte[2];
        for (int i = 0; i < 2; i++) {
            bb[index.value + i] = new Integer(temp & 0xff).byteValue(); // 将最高位保存在最
            temp = temp >> 8; // 向右移8位
        }

        index.value += 2;
    }

    /**
     * 字节到字符转换
     *
     * @param b
     * @return
     */
    public static char getChar(byte[] b, Box<Integer> index) {
        int s = 0;
        if (b[index.value + 1] > 0)
            s += b[index.value + 1];
        else
            s += 256 + b[index.value + 0];
        s *= 256;
        if (b[index.value + 0] > 0)
            s += b[index.value + 1];
        else
            s += 256 + b[index.value + 0];
        char ch = (char) s;
        index.value += 2;
        return ch;
    }

    /**
     * float转换byte
     *
     * @param bb
     * @param x
     */
    public static void putFloat(byte[] bb, float x, Box<Integer> index) {
        // byte[] b = new byte[4];
        int l = Float.floatToIntBits(x);
        for (int i = 0; i < 4; i++) {
            bb[index.value + i] = new Integer(l).byteValue();
            l = l >> 8;
        }
        index.value += 4;
    }

    /**
     * 通过byte数组取得float
     *
     * @return
     */
    public static float getFloat(byte[] b, Box<Integer> index) {
        int l;
        l = b[index.value + 0];
        l &= 0xff;
        l |= ((long) b[index.value + 1] << 8);
        l &= 0xffff;
        l |= ((long) b[index.value + 2] << 16);
        l &= 0xffffff;
        l |= ((long) b[index.value + 3] << 24);
        index.value += 4;
        return Float.intBitsToFloat(l);
    }

    /**
     * double转换byte
     *
     * @param bb
     * @param x
     */
    public static void putDouble(byte[] bb, double x, Box<Integer> index) {
        // byte[] b = new byte[8];
        long l = Double.doubleToLongBits(x);
        for (int i = 0; i < 4; i++) {
            bb[index.value + i] = new Long(l).byteValue();
            l = l >> 8;
        }
        index.value += 8;
    }

    /**
     * 通过byte数组取得float
     *
     * @return
     */
    public static double getDouble(byte[] b, Box<Integer> index) {
        long l;
        l = b[0];
        l &= 0xff;
        l |= ((long) b[1] << 8);
        l &= 0xffff;
        l |= ((long) b[2] << 16);
        l &= 0xffffff;
        l |= ((long) b[3] << 24);
        l &= 0xffffffffl;
        l |= ((long) b[4] << 32);
        l &= 0xffffffffffl;
        l |= ((long) b[5] << 40);
        l &= 0xffffffffffffl;
        l |= ((long) b[6] << 48);
        l &= 0xffffffffffffffl;
        l |= ((long) b[7] << 56);
        index.value += 8;
        return Double.longBitsToDouble(l);
    }

    public static void putString(byte[] bb, String str, Box<Integer> index) {
        byte[] data = str.getBytes();
        for (int i = 0; i < data.length; i++) {
            bb[i] = data[i];
        }
        index.value += data.length;
    }

    public static String getString(byte[] bb, Box<Integer> index, int length) {
        byte[] data = new byte[length];
        int realLength = 0;
        for (int i = 0; i < data.length; i++) {
            if (bb[index.value + i] == 0) {
                realLength = i;
                break;
            }
            data[i] = bb[index.value + i];
        }

        byte[] realData = Arrays.copyOf(data, realLength);
        index.value += length;
        return new String(realData);
    }
}
